<?php namespace WoXuanWang\MemberOrder\Controllers;

use WoXuanWang\MemberOrder\Requests\CreateMemberOrderRequest;
use WoXuanWang\MemberOrder\Requests\UpdateMemberOrderRequest;
use App\Http\Requests\IndexRequest;
use App\Http\Requests\ShowRequest;
use Illuminate\Http\JsonResponse;
use WoXuanWang\MemberOrder\Repositories\MemberOrderRepository;
use zgldh\Scaffold\AppBaseController;

class MemberOrderController extends AppBaseController
{

    public function __construct(MemberOrderRepository $memberorderRepository)
    {
        $this->repository = $memberorderRepository;
        $this->middleware("auth");
    }

    /**
     * Display a listing of the MemberOrder.
     *
     * @param Request $request
     * @return Response
     */
    public function index(IndexRequest $request)
    {
          $with = $request->getWith();
          $data = $this->repository->datatables(null, $with);
          return $data;
    }

    /**
     * Store a newly created MemberOrder in storage.
     *
     * @param CreateMemberOrderRequest $request
     * @return JsonResponse
     */
    public function store(CreateMemberOrderRequest $request)
    {
        $input = $request->all();

        $memberorder = $this->repository->create($input);

        return $this->sendResponse($memberorder, 'MemberOrder saved successfully.');
    }

    /**
     * Display the specified MemberOrder.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function show($id, ShowRequest $request)
    {
        $this->repository->with($request->getWith());
        $memberorder = $this->repository->findWithoutFail($id);

        if (empty($memberorder)) {
            return $this->sendError('MemberOrder not found');
        }

        return $this->sendResponse($memberorder, '');
    }

    /**
     * Update the specified MemberOrder in storage.
     *
     * @param  int $id
     * @param UpdateMemberOrderRequest $request
     *
     * @return JsonResponse
     */
    public function update($id, UpdateMemberOrderRequest $request)
    {
        $memberorder = $this->repository->findWithoutFail($id);

        if (empty($memberorder)) {
            return $this->sendError('MemberOrder not found');
        }

        $memberorder = $this->repository->update($request->all(), $id);

        return $this->sendResponse($memberorder, 'MemberOrder updated successfully.');
    }

    /**
     * Remove the specified MemberOrder from storage.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function destroy($id)
    {
        $memberorder = $this->repository->findWithoutFail($id);

        if (empty($memberorder)) {
            return $this->sendError('MemberOrder not found');
        }

        $this->repository->delete($id);
        return $this->sendResponse($memberorder, 'MemberOrder deleted successfully.');
    }
}
